package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.widget.MsgView;
import com.epfresh.bean.ProductCategory;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */

public class CategoryGoodsAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;


    private List<ProductCategory> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public CategoryGoodsAdapter(Context context, List<ProductCategory> list) {
        this.context = context;
        this.list = list;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView tv = null;
        MsgView msgView = null;

        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_all_category, null, false);
            tv = (TextView) view.findViewById(R.id.tv_category);
            msgView = (MsgView) view.findViewById(R.id.msg);
            view.setTag(tv);
            view.setTag(R.id.item_key_position, msgView);
        } else {
            tv = (TextView) view.getTag();
            msgView = (MsgView) view.getTag(R.id.item_key_position);
        }



        ProductCategory p = list.get(i);

        if (p.isSelect()) {
            tv.setBackgroundResource(android.R.color.white);
            tv.setTextColor(context.getResources().getColor(R.color.green_little));
        } else {
            tv.setBackgroundColor(0xf1f1f2);
            tv.setTextColor(Color.parseColor("#626262"));
        }
        tv.setTag(R.id.item_key_position, i);
        tv.setOnClickListener(this);
        tv.setText(p.getName());

        msgView.setVisibility(View.VISIBLE);
        msgView.setText(p.getCount() + "");
        String id = p.getId();
        if ("-2".equals(id)) {
            if (((int) p.getCount()) > 0) {
                show(msgView, 0, true);
            } else {
                show(msgView, 0, false);
            }
        } else {
            show(msgView, (int) p.getCount(), false);

        }

        return view;
    }

    public void show(MsgView msgView, int num, boolean isShow) {
        if (msgView == null) {
            return;
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) msgView.getLayoutParams();
        DisplayMetrics dm = msgView.getResources().getDisplayMetrics();
        msgView.setVisibility(View.VISIBLE);
        if (num <= 0) {//圆点,设置默认宽高
            msgView.setStrokeWidth(0);
            msgView.setText("");

            lp.width = (int) (9 * dm.density);
            lp.height = (int) (9 * dm.density);
            msgView.setLayoutParams(lp);
            if (!isShow) {
                msgView.setVisibility(View.GONE);
            }
        } else {
            lp.height = (int) (15 * dm.density);
            if (num > 0 && num < 10) {//圆
                lp.width = (int) (15 * dm.density);
                msgView.setText(num + "");
            } else if (num > 9 && num < 100) {//圆角矩形,圆角是高度的一半,设置默认padding
                lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                msgView.setPadding((int) (5 * dm.density), 0, (int) (5 * dm.density), 0);
                msgView.setText(num + "");
            } else {//数字超过两位,显示99+
                lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                msgView.setPadding((int) (5 * dm.density), 0, (int) (5 * dm.density), 0);
                msgView.setText("99+");
            }
            msgView.setLayoutParams(lp);
        }
    }
    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            int position = (int) o;
            updateView(v, position);

        }

    }

    private void updateView(View v, int position) {
        for (int i = 0; i < list.size(); i++) {
            ProductCategory p = list.get(i);
            if (i == position) {
                p.setSelect(true);
            } else {
                p.setSelect(false);
            }
        }

        notifyDataSetChanged();

        if (onItemClickListener != null)
            onItemClickListener.onItemClick(null, v, position, position);
    }

    public void updateView(int position) {
        for (int i = 0; i < list.size(); i++) {
            ProductCategory p = list.get(i);
            if (i == position) {
                p.setSelect(true);
            } else {
                p.setSelect(false);
            }
        }

        notifyDataSetChanged();
    }


}
